package interceptcrossing.ui;

import interceptcrossing.Abscissa;
import interceptcrossing.Intercept;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.util.Collection;

/**
 * @author: Igor Botyan
 * @date: May 7, 2010
 */
public class SweptLineStatusPanel extends JPanel {

	private DebugWindowDirector windowDirector;
	private DefaultTableModel aboveInterceptsTableModel;
	private DefaultTableModel currentInterceptsTableModel;
	private DefaultTableModel underInterceptsTableModel;
	private JTable aboveInterceptsTable;
	private JTable currentInterceptsTable;
	private JTable underInterceptsTable;

	public SweptLineStatusPanel(DebugWindowDirector windowDirector) {
		if (windowDirector == null) {
			throw new NullPointerException("Window director cannot be null");
		}

		this.windowDirector = windowDirector;

		this.aboveInterceptsTableModel = new DefaultTableModel(new String[][]{}, new String[]{"№", "Отрезок"});
		this.currentInterceptsTableModel = new DefaultTableModel(new String[][]{}, new String[]{"№", "Отрезок"});
		this.underInterceptsTableModel = new DefaultTableModel(new String[][]{}, new String[]{"№", "Отрезок"});

		this.aboveInterceptsTable = new JTable(this.aboveInterceptsTableModel);
		this.aboveInterceptsTable.setFillsViewportHeight(true);
		this.currentInterceptsTable = new JTable(this.currentInterceptsTableModel);
		this.currentInterceptsTable.setFillsViewportHeight(true);
		this.underInterceptsTable = new JTable(this.underInterceptsTableModel);
		this.underInterceptsTable.setFillsViewportHeight(true);

		this.initLayout();
		this.initActions();
	}

	private void initLayout() {
		super.setLayout(new GridBagLayout());

		GridBagConstraints constraints = new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.EAST,
			GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

		super.add(new JLabel("Отрезки выше текущих"), constraints);

		constraints = new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0, GridBagConstraints.EAST,
			GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);

		super.add(new JScrollPane(this.aboveInterceptsTable), constraints);

		constraints = new GridBagConstraints(1, 3, 1, 1, 1.0, 0.0, GridBagConstraints.EAST,
			GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

		super.add(new JLabel("Текущие отрезки"), constraints);

		constraints = new GridBagConstraints(1, 4, 1, 1, 1.0, 1.0, GridBagConstraints.EAST,
			GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);

		super.add(new JScrollPane(this.currentInterceptsTable), constraints);

		constraints = new GridBagConstraints(1, 5, 1, 1, 1.0, 0.0, GridBagConstraints.EAST,
			GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 0, 0);

		super.add(new JLabel("Отрезки ниже текущих"), constraints);

		constraints = new GridBagConstraints(1, 6, 1, 1, 1.0, 1.0, GridBagConstraints.EAST,
			GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0);

		super.add(new JScrollPane(this.underInterceptsTable), constraints);
	}

	private void initActions() {
		this.windowDirector.addListener(new DebugWindowDirector.Listener() {

			public void currentAbscissaChanged(Abscissa abscissa, Collection<Intercept> aboveIntercepts,
			                                   Collection<Intercept> underIntercepts) {

				java.util.List<Intercept> intercepts = windowDirector.getIntercepts();

				for (int i = aboveInterceptsTableModel.getRowCount() - 1; i >= 0; i--) {
					aboveInterceptsTableModel.removeRow(i);
				}

				for (int i = currentInterceptsTableModel.getRowCount() - 1; i >= 0; i--) {
					currentInterceptsTableModel.removeRow(i);
				}

				for (int i = underInterceptsTableModel.getRowCount() - 1; i >= 0; i--) {
					underInterceptsTableModel.removeRow(i);
				}

				if (abscissa == null) {
					return;
				}

				for (Intercept intercept : aboveIntercepts) {
					aboveInterceptsTableModel.addRow(new String[]{Integer.toString(intercepts.indexOf(intercept) + 1),
						intercept.toString()});
				}

				currentInterceptsTableModel.addRow(new String[]{Integer.toString(intercepts.indexOf(abscissa.intercept) + 1),
					abscissa.intercept.toString()});

				if (abscissa.secondIntercept != null) {
					currentInterceptsTableModel.addRow(new String[]{Integer.toString(intercepts.indexOf(abscissa.secondIntercept) + 1), abscissa.secondIntercept.toString()});
				}

				for (Intercept intercept : underIntercepts) {
					underInterceptsTableModel.addRow(new String[]{Integer.toString(intercepts.indexOf(intercept) + 1), intercept.toString()});
				}
			}
		});
	}
}
